#include "mat_ui_msgbox.h"
#include "mat_gamerule.h"
#include "mat_tutorial.h"

#include "gameflow/ui_popup.h"

MATUIMsgBox::MATUIMsgBox() : MATUIPopupAnimator("ui/ok.ui","WidgetOK"),
							m_pUIMsg(NULL),
							m_OkAction(ACTION_NONE),
							m_Duration(0),
							m_Delay(0)
{
	
	if(m_pPopup)
	{
		m_pAnimateWidget = m_pPopup->GetWidget("BG");
		MATUIPopupAnimatorMgr::Instance().AddPopupAnimator(MATUIPopupAnimatorMgr::POPUP_NAMES_MSGBOX,this);

		m_pUIMsg = m_pPopup->GetWidget("Msg");

		m_pPopup->SetWidgetFunctor("BG",UI_DELEGATE(MATUIMsgBox, OnButtonOK));

	}

	m_Type = ANIMATION_SLIDEFROMTOP;
}


void		MATUIMsgBox::ShowExtra(float Delay, const std::string& Msg, OK_ACTION Action)
{
	if(m_pUIMsg)
		m_pUIMsg->SetTextA(Msg);

	m_OkAction = Action;

	m_Delay = Delay;
	//Show(NULL);
}

void	MATUIMsgBox::Tick(float dt)
{
	MATUIPopupAnimator::Tick(dt);

	if(m_Delay > 0)
	{
		m_Delay-= dt;
		if( m_Delay <= 0)
		{
			MATUIPopupAnimatorMgr::Instance().Show(MATUIPopupAnimatorMgr::POPUP_NAMES_MSGBOX);
			m_Duration = 3.5f;
		}
	}
	else if ( m_Duration > 0)
	{
		m_Duration -= dt;
		if( m_Duration <= 0)
		{
			Hide();
			MATUIAnimator::Instance().Add(m_pAnimateWidget,MATAnimatedUI::ANIMATE_STYLE_HIDE_TOTOP,0, this);
		}
	}
}



void DELEGATE_CALL	MATUIMsgBox::OnButtonOK(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	if(m_Duration <= 0.00001f)
	{
		m_pPopup->Hide(NULL);

		MATTutorial* pTutorial = MATGameRule::Instance().GetTutorial();
		if(MATGameRule::Instance().IsTutorialActive() && pTutorial)
		{
			pTutorial->StartImediately();
		}
		return;
	}

	m_Delay = -1;
	m_Duration = 0.00001f;
	//Hide();
	//MATUIAnimator::Instance().Add(m_pAnimateWidget,MATAnimatedUI::ANIMATE_STYLE_HIDE_TOTOP,0, this);
	

	switch(m_OkAction)
	{
		case ACTION_NEWGAME:
			//MATGameRule::Instance().NewGame();
		break;
	}
	
	m_OkAction = ACTION_NONE;
}

void			MATUIMsgBox::OnAnimatedUIFinish()
{
	
	if(m_pPopup)
		m_pPopup->Hide(NULL);

	MATTutorial* pTutorial = MATGameRule::Instance().GetTutorial();
	if(MATGameRule::Instance().IsTutorialActive() && pTutorial)
	{
		pTutorial->StartImediately();
	}
}

